package clients;

/*
This file is part of Open Grid Job Management System (OGJMS).

OGJMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

OGJMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OGJMS.  If not, see <http://www.gnu.org/licenses/>.

The entire and newest source code can be found at http://code.google.com/p/ogjms/  
*/

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.filechooser.FileFilter;

import org.globus.wsrf.NoSuchResourceException;

import JobManagementUtils.FTResults;
import JobManagementUtils.FileManager;

public class ClientGrafic extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6422459713556898799L;

	private JTabbedPane jTabbedPane = null;
	private JPanel ClientPanel = null;

	private JPanel ConfigurationPanel = null;
	private JTextField GridHostTextField = null;
	private JTextField GridUser = null;
	private JTextField UserFolder = null;
	private JTextField GridFolder = null;
	private JPasswordField PasswordField = null;
	private JLabel jLabel = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JButton OKButton = null;
	private JLabel jLabel51 = null;
	private JTextField ProxyFile1 = null;
	private JLabel jLabel1 = null;
	private JTextField FisierRSL = null;
	private JButton AlegeButton = null;
	private JLabel jLabel6 = null;
	private JTextField FisiereIntrareTextField = null;
	private JButton AlegeFisiereIntrareButton = null;
	private JList FisiereIntrareList = null;
	private JLabel jLabel7 = null;
	private JTextField FisiereIesireTextField = null;
	private JButton FisiereIesireButton = null;
	private JList FisiereIesireList = null;
	private DefaultListModel FisiereIesireListModel = null;
	private DefaultListModel FisiereIntrareListModel = null;
	private JButton StergeIntrareButton = null;
	private JButton StergeIesireButton = null;
	private JList ReportList = null;
	private DefaultListModel ReportListModel;
	private JButton TransferInButton = null;
	private JButton TransferOutButton = null;
	private JButton DeleteButton = null;
	private JButton StartJobButton = null;
	private JButton StopJobButton1 = null;
	private JButton StergeButton = null;
	private JPanel JobPanel = null;
	private JLabel jLabel8 = null;
	private JTextField NumeJobTextField = null;
	private JButton Creeaza = null;
	private JLabel jLabel9 = null;
	private JTextField JobDirTextField = null;
	private JComboBox JobListComboBox = null;

	private JScrollPane FisiereIntrareScrollpane = null;
	private JScrollPane FisiereIesireScrollpane = null;

	private Timer timer = null;

	private FileManager manager = null;

	private boolean fileInTransferRunning = false;

	private boolean fileOutTransferRunning = false;

	private boolean fileDeleteRunning = false;

	private FTResults fileInResults;

	private FTResults fileOutResults;

	private FTResults deleteResults;

	/**
	 * This method initializes
	 * 
	 */
	public ClientGrafic() {
		super();
		initialize();

	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setTitle("Client Management Joburi");
		this.setContentPane(getJTabbedPane());
		this.setSize(new Dimension(700, 730));
		getTimer();
	}

	/**
	 * This method initializes jTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("Client", null, getClientPanel(), null);
			jTabbedPane.addTab("Configuratie", null, getConfigurationPanel(),
					null);
			jTabbedPane.addTab("Creare Job Manager", null, getJobPanel(), null);
			jTabbedPane.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusGained(java.awt.event.FocusEvent e) {
					getGridHostTextField().setText(Optiuni.getHost());
					getGridUser().setText(Optiuni.getUser());
					getGridFolder().setText(Optiuni.getHostURL());
					getUserFolder().setText(Optiuni.getUserURL());
					getProxyFile1().setText(Optiuni.getProxyFile());
					getJobDirTextField().setText(Optiuni.getJobFolder());
				}
			});
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes ClientPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getClientPanel() {
		if (ClientPanel == null) {
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(20, 250, 120, 20));
			jLabel7.setText("Fisiere de iesire:");
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(20, 80, 120, 20));
			jLabel6.setText("Fisiere de intrare:");
			jLabel1 = new JLabel();
			jLabel1.setText("Descriptor de Job:");
			jLabel1.setBounds(new Rectangle(20, 50, 120, 20));
			ClientPanel = new JPanel();
			ClientPanel.setLayout(null);
			ClientPanel.add(jLabel1, null);
			ClientPanel.add(getFisierRSL(), null);
			ClientPanel.add(getAlegeButton(), null);
			ClientPanel.add(jLabel6, null);
			ClientPanel.add(getFisiereIntrareTextField(), null);
			ClientPanel.add(getAlegeFisiereIntrareButton(), null);
			getFisiereIntrareList();
			ClientPanel.add(getFisiereIntrareScrollpane(), null);
			ClientPanel.add(jLabel7, null);
			ClientPanel.add(getFisiereIesireTextField(), null);
			ClientPanel.add(getFisiereIesireButton(), null);
			getFisiereIesireList();
			ClientPanel.add(getFisiereIesireScrollpane(), null);

			ClientPanel.add(getStergeIntrareButton(), null);
			ClientPanel.add(getStergeIesireButton(), null);
			ClientPanel.add(getTransferInButton(), null);
			ClientPanel.add(getTransferOutButton(), null);
			ClientPanel.add(getDeleteButton1(), null);
			ClientPanel.add(getReportList(), null);
			ClientPanel.add(getStartJobButton(), null);
			ClientPanel.add(getStopJobButton1(), null);
			ClientPanel.add(getStergeButton(), null);
			ClientPanel.add(getJobListComboBox(), null);
		}
		return ClientPanel;
	}

	/**
	 * This method initializes ConfigurationPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getConfigurationPanel() {
		if (ConfigurationPanel == null) {
			jLabel9 = new JLabel();
			jLabel9.setBounds(new Rectangle(10, 200, 110, 20));
			jLabel9.setText("Director de Joburi");
			jLabel51 = new JLabel();
			jLabel51.setBounds(new Rectangle(10, 170, 110, 20));
			jLabel51.setText("Fisier Proxy");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(10, 140, 110, 20));
			jLabel5.setText("Director Host");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(10, 110, 110, 20));
			jLabel4.setText("Director Client");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(10, 80, 110, 20));
			jLabel3.setText("Parola");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(10, 50, 110, 20));
			jLabel2.setText("Utilizator");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 20, 110, 20));
			jLabel.setText("Grid Host");
			ConfigurationPanel = new JPanel();
			ConfigurationPanel.setLayout(null);
			ConfigurationPanel.add(getGridHostTextField(), null);
			ConfigurationPanel.add(getGridUser(), null);
			ConfigurationPanel.add(getUserFolder(), null);
			ConfigurationPanel.add(getGridFolder(), null);
			ConfigurationPanel.add(getPasswordField(), null);
			ConfigurationPanel.add(jLabel, null);
			ConfigurationPanel.add(jLabel2, null);
			ConfigurationPanel.add(jLabel3, null);
			ConfigurationPanel.add(jLabel4, null);
			ConfigurationPanel.add(jLabel5, null);
			ConfigurationPanel.add(getOKButton(), null);
			ConfigurationPanel.add(jLabel51, null);
			ConfigurationPanel.add(getProxyFile1(), null);
			ConfigurationPanel.add(jLabel9, null);
			ConfigurationPanel.add(getJobDirTextField(), null);
		}
		return ConfigurationPanel;
	}

	private Timer getTimer() {
		if (timer == null) {
			timer = new Timer(10000, new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					update();

				}
			});
		}
		timer.start();
		return timer;
	}

	protected void update() {
		ReportListModel = new DefaultListModel();
		if (fileInTransferRunning) {
			fileInResults = manager.processResults("Copiere fisiere pe server",
					manager.getFileInSize());
			if (fileInResults != null) {
				if (fileInResults.isPending()) {
					ReportListModel
							.addElement("Copiere fisiere pe server: in progress");
				} else {
					System.out.println(fileInResults.getFinalResults());
					// ReportListModel.addElement(fileInResults.getFinalResults());
					fileInTransferRunning = false;
					enableAllButtons();
				}
			}
		} else {
			if (fileInResults != null) {
				ReportListModel
						.addElement(fileInResults.isTransferSuccessful() ? "Copiere fisiere pe server: OK"
								: "Copiere fisiere pe server: Esuat");
			}
		}

		if (fileOutTransferRunning) {
			fileOutResults = manager.processResults(
					"Copiere fisiere de pe server", manager.getFileOutSize());
			if (fileOutResults != null) {
				if (fileOutResults.isPending()) {
					ReportListModel
							.addElement("Copiere fisiere de pe server: in progress");
				} else {
					// ReportListModel.addElement(fileOutResults.getFinalResults());
					disableOtherButtons(TransferInButton);
					fileOutTransferRunning = false;
					enableAllButtons();
				}
			}
		} else {
			if (fileOutResults != null) {
				ReportListModel
						.addElement(fileOutResults.isTransferSuccessful() ? "Copiere fisiere de pe server: OK"
								: "Copiere fisiere de pe server:Esuata");
			}
		}

		if (fileDeleteRunning) {
			deleteResults = manager.processResults(
					"Stergere fisiere de pe server", manager.getFileOutSize());
			if (deleteResults != null) {
				if (deleteResults.isPending()) {
					ReportListModel
							.addElement("Stergere fisiere de pe server: in progress");
				} else {
					// ReportListModel.addElement(fileOutResults.getFinalResults());
					disableOtherButtons(DeleteButton);
					fileDeleteRunning = false;
					enableAllButtons();
				}
			}
		} else {
			if (deleteResults != null) {
				ReportListModel
						.addElement(deleteResults.isTransferSuccessful() ? "Stergere fisiere de pe server: OK"
								: "Stergere fisiere de pe server:Esuata");
			}
		}

		if (JobListComboBox.getModel().getSelectedItem() != null) {
			try {
				JobManagementServiceProxyClass proxyClass = new JobManagementServiceProxyClass(
						Optiuni.getJobFolderFullPath() + "/"
								+ JobListComboBox.getModel().getSelectedItem());
				ReportListModel
						.addElement("Statusul Managerului: "
								+ getJobManagerStatus(proxyClass
										.getJobManagerStatus()));
				ReportListModel
						.addElement("Statusul Jobului: "
								+ (proxyClass.getJobStatus() == null ? "Nu a fost pornit"
										: proxyClass.getJobStatus()));
			} catch (Exception noSuchRes) {
				if (noSuchRes instanceof NoSuchResourceException) {
					JobManagementServiceProxyClass proxyClass = new JobManagementServiceProxyClass(
							Optiuni.getJobFolderFullPath()
									+ "/"
									+ JobListComboBox.getModel()
											.getSelectedItem());

					proxyClass.destroy();
					ClientPanel.remove(JobListComboBox);
					JobListComboBox = null;
					ClientPanel.add(getJobListComboBox(), null);
					ClientPanel.repaint();
				}
			}
		}
		ReportList.setModel(ReportListModel);

	}

	public String getJobManagerStatus(int status) {
		switch (status) {
		case -1:
			return "Oprit";
		case 0:
			return "Oprit";
		case 1:
			return "Activ";
		case 2:
			return "Blocat";
		case 3:
			return "In asteptare";
		case 4:
			return "In asteptare cronometrata";
		case 5:
			return "Terminat";
		}
		return "Nu se stie starea";
	}

	/**
	 * This method initializes GridHostTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGridHostTextField() {
		if (GridHostTextField == null) {
			GridHostTextField = new JTextField();
			GridHostTextField.setBounds(new Rectangle(120, 20, 500, 20));
		}
		return GridHostTextField;
	}

	private JComboBox getJobListComboBox() {
		if (JobListComboBox == null) {
			String[] filesList = new String[1];
			try {
				boolean ok = false;
				File dir = new File(Optiuni.getJobFolderFullPath());
				if (!dir.exists()){
					dir.mkdir();
				}
				filesList = dir.list(new JobManagementUtils.FileFilter());

			} catch (Exception e) {
				e.printStackTrace();
			}
			JobListComboBox = new JComboBox(filesList);
			JobListComboBox.setBounds(new Rectangle(250, 20, 180, 20));

		}
		return JobListComboBox;

	}

	/**
	 * This method initializes GridUser
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGridUser() {
		if (GridUser == null) {
			GridUser = new JTextField();
			GridUser.setBounds(new Rectangle(120, 50, 500, 20));
		}
		return GridUser;
	}

	/**
	 * This method initializes UserFolder
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getUserFolder() {
		if (UserFolder == null) {
			UserFolder = new JTextField();
			UserFolder.setBounds(new Rectangle(120, 110, 500, 20));
		}
		return UserFolder;
	}

	/**
	 * This method initializes GridFolder
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGridFolder() {
		if (GridFolder == null) {
			GridFolder = new JTextField();
			GridFolder.setBounds(new Rectangle(120, 140, 500, 20));
		}
		return GridFolder;
	}

	/**
	 * This method initializes PasswordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getPasswordField() {
		if (PasswordField == null) {
			PasswordField = new JPasswordField();
			PasswordField.setBounds(new Rectangle(120, 80, 500, 20));
		}
		return PasswordField;
	}

	/**
	 * This method initializes OKButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOKButton() {
		if (OKButton == null) {
			OKButton = new JButton("Aceptare");
			OKButton.setBounds(new Rectangle(38, 299, 498, 25));
			OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					Optiuni.setHost(getGridHostTextField().getText());
					Optiuni.setUser(getGridUser().getText());
					Optiuni
							.setPassword(getPasswordField().getPassword().length > 0 ? getPasswordField()
									.getPassword().toString()
									: Optiuni.getPassword());
					Optiuni.setHostURL(getGridFolder().getText());
					Optiuni.setUserURL(getUserFolder().getText());
					Optiuni.setProxyFile(getProxyFile1().getText());
					Optiuni.setJobFolder(getJobDirTextField().getText());
				}
			});
		}
		return OKButton;
	}

	/**
	 * This method initializes ProxyFile1
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getProxyFile1() {
		if (ProxyFile1 == null) {
			ProxyFile1 = new JTextField();
			ProxyFile1.setBounds(new Rectangle(120, 170, 500, 20));
		}
		return ProxyFile1;
	}

	/**
	 * This method initializes FisierRSL
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getFisierRSL() {
		if (FisierRSL == null) {
			FisierRSL = new JTextField();
			FisierRSL.setBounds(new Rectangle(150, 50, 390, 20));
		}
		return FisierRSL;
	}

	/**
	 * This method initializes AlegeButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAlegeButton() {
		if (AlegeButton == null) {
			AlegeButton = new JButton();
			AlegeButton.setBounds(new Rectangle(550, 50, 120, 20));
			AlegeButton.setText("Alege");
			AlegeButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					JFileChooser fc = new JFileChooser();
					fc.setFileFilter(new FileFilter() {

						@Override
						public String getDescription() {

							return "RSL Files";
						}

						@Override
						public boolean accept(File f) {
							if (f.getName().endsWith(".rsl") || f.isDirectory())
								return true;
							else
								return false;
						}
					});
					fc.showOpenDialog(AlegeButton);
					File file = fc.getSelectedFile();
					if (file != null) {
						getFisierRSL().setText(
								fc.getSelectedFile().getAbsolutePath());
					}

				}
			});
		}
		return AlegeButton;
	}

	/**
	 * This method initializes FisiereIntrareTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getFisiereIntrareTextField() {
		if (FisiereIntrareTextField == null) {
			FisiereIntrareTextField = new JTextField();
			FisiereIntrareTextField.setBounds(new Rectangle(150, 80, 390, 20));
		}
		return FisiereIntrareTextField;
	}

	/**
	 * This method initializes AlegeFisiereIntrareButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAlegeFisiereIntrareButton() {
		if (AlegeFisiereIntrareButton == null) {
			AlegeFisiereIntrareButton = new JButton();
			AlegeFisiereIntrareButton
					.setBounds(new Rectangle(550, 80, 120, 20));
			AlegeFisiereIntrareButton.setText("Adauga");
			AlegeFisiereIntrareButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {
							if (!getFisiereIntrareTextField().getText().trim()
									.equals("")) {
								FisiereIntrareListModel.add(
										FisiereIntrareListModel.getSize(),
										getFisiereIntrareTextField().getText());

								getFisiereIntrareList().setModel(
										FisiereIntrareListModel);
							}

						}
					});
		}
		return AlegeFisiereIntrareButton;
	}

	/**
	 * This method initializes FisiereIntrareList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getFisiereIntrareList() {
		if (FisiereIntrareList == null) {
			FisiereIntrareList = new JList();
			FisiereIntrareList.setBounds(new Rectangle(20, 110, 520, 120));
			FisiereIntrareList.setBorder(BorderFactory.createEtchedBorder());
			FisiereIntrareList.setVisibleRowCount(5);
			FisiereIntrareList.setFixedCellHeight(20);

			FisiereIntrareListModel = new DefaultListModel();
			FisiereIntrareList.setModel(FisiereIntrareListModel);
			FisiereIntrareList
					.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			FisiereIntrareList.setLayoutOrientation(JList.VERTICAL);

		}
		return FisiereIntrareList;
	}

	/**
	 * This method initializes FisiereIesireTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getFisiereIesireTextField() {
		if (FisiereIesireTextField == null) {
			FisiereIesireTextField = new JTextField();
			FisiereIesireTextField.setBounds(new Rectangle(150, 250, 390, 20));
		}
		return FisiereIesireTextField;
	}

	/**
	 * This method initializes FisiereIesireButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getFisiereIesireButton() {
		if (FisiereIesireButton == null) {
			FisiereIesireButton = new JButton();
			FisiereIesireButton.setBounds(new Rectangle(550, 250, 120, 20));
			FisiereIesireButton.setText("Adauga");
			FisiereIesireButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {
							if (!getFisiereIesireTextField().getText().trim()
									.equals("")) {
								FisiereIesireListModel.add(
										FisiereIesireListModel.getSize(),
										getFisiereIesireTextField().getText());
								getFisiereIesireList().setModel(
										FisiereIesireListModel);
							}
						}
					});
		}
		return FisiereIesireButton;
	}

	/**
	 * This method initializes FisiereIesireList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getFisiereIesireList() {
		if (FisiereIesireList == null) {
			FisiereIesireList = new JList();

			FisiereIesireList.setBounds(new Rectangle(20, 280, 520, 120));
			FisiereIesireList.setBorder(BorderFactory.createEtchedBorder());
			FisiereIesireList.setVisibleRowCount(5);
			FisiereIesireList.setFixedCellHeight(20);

			FisiereIesireListModel = new DefaultListModel();
			FisiereIesireList.setModel(FisiereIesireListModel);
			FisiereIesireList
					.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			FisiereIesireList.setLayoutOrientation(JList.VERTICAL);
		}
		return FisiereIesireList;
	}

	private JScrollPane getFisiereIntrareScrollpane() {
		if (FisiereIntrareScrollpane == null) {
			FisiereIntrareScrollpane = new JScrollPane(FisiereIntrareList);
			FisiereIntrareScrollpane.setPreferredSize(new Dimension(520, 120));
			FisiereIntrareScrollpane.setBounds(20, 110, 520, 120);
		}
		return FisiereIntrareScrollpane;
	}

	private JScrollPane getFisiereIesireScrollpane() {
		if (FisiereIesireScrollpane == null) {
			FisiereIesireScrollpane = new JScrollPane(FisiereIesireList);
			FisiereIesireScrollpane.setPreferredSize(new Dimension(520, 120));
			FisiereIesireScrollpane.setBounds(20, 280, 520, 120);
		}
		return FisiereIesireScrollpane;
	}

	/**
	 * This method initializes StergeIntrareButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStergeIntrareButton() {
		if (StergeIntrareButton == null) {
			StergeIntrareButton = new JButton();
			StergeIntrareButton.setBounds(new Rectangle(550, 110, 120, 20));
			StergeIntrareButton.setText("Sterge");
			StergeIntrareButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {

							if (getFisiereIntrareList().getSelectedIndex() >= 0) {
								FisiereIntrareListModel
										.remove(getFisiereIntrareList()
												.getSelectedIndex());
							}

							getFisiereIntrareList().setModel(
									FisiereIntrareListModel);
						}
					});
		}
		return StergeIntrareButton;
	}

	/**
	 * This method initializes StergeIesireButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStergeIesireButton() {
		if (StergeIesireButton == null) {
			StergeIesireButton = new JButton();
			StergeIesireButton.setBounds(new Rectangle(550, 300, 120, 20));
			StergeIesireButton.setText("Sterge");
			StergeIesireButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {
							if (getFisiereIesireList().getSelectedIndex() >= 0)
								FisiereIesireListModel
										.remove(getFisiereIesireList()
												.getSelectedIndex());
							getFisiereIesireList().setModel(
									FisiereIesireListModel);
						}
					});
		}
		return StergeIesireButton;
	}

	/**
	 * This method initializes ReportList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getReportList() {
		if (ReportList == null) {
			ReportList = new JList();

			ReportListModel = new DefaultListModel();
			ReportList.setModel(ReportListModel);
			ReportList
					.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			ReportList.setBounds(new Rectangle(20, 450, 320, 200));
			ReportList.setLayoutOrientation(JList.VERTICAL);
			ReportList.setBorder(BorderFactory.createEtchedBorder());
			ReportList.setVisibleRowCount(5);
			ReportList.setFixedCellHeight(20);

		}
		return ReportList;
	}

	private FileManager getManager() {
		if (manager == null) {
			manager = new FileManager(Optiuni.getProxyFile(), Optiuni.getUserURL(), Optiuni
							.getHostURL(), Optiuni.getDelegationURL(), Optiuni
							.getReliableFileTransferFactory(), Optiuni
							.getReliableFileTransfer());
		}
		return manager;
	}

	/**
	 * This method initializes TransferInButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getTransferInButton() {
		if (TransferInButton == null) {
			TransferInButton = new JButton();
			TransferInButton.setBounds(new Rectangle(30, 410, 150, 30));
			TransferInButton.setText("Copie pe server");
			TransferInButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {

							ArrayList<String> list = new ArrayList<String>();
							if (getFisiereIntrareList().getModel().getSize() > 0) {
								for (int i = 0; i < getFisiereIntrareList()
										.getModel().getSize(); i++) {
									list.add((String) (getFisiereIntrareList()
											.getModel().getElementAt(i)));
								}
								// getFisiereIesireList().setEnabled(false);
								disableOtherButtons(TransferInButton);
								getManager().setFileIn(list);
								getManager().submitTranferInJob();
								if (!getTimer().isRunning()) {
									getTimer().start();
								}
								fileInTransferRunning = true;
							}
							// disableOtherButtons(TransferInButton);
						}
					});
		}
		return TransferInButton;
	}

	/**
	 * This method initializes TransferOutButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getTransferOutButton() {
		if (TransferOutButton == null) {
			TransferOutButton = new JButton();
			TransferOutButton.setBounds(new Rectangle(185, 410, 150, 30));
			TransferOutButton.setText("Copie de pe server");
			TransferOutButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseClicked(java.awt.event.MouseEvent e) {
							ArrayList<String> list = new ArrayList<String>();
							if (getFisiereIesireList().getModel().getSize() > 0) {
								for (int i = 0; i < getFisiereIesireList()
										.getModel().getSize(); i++) {
									list.add((String) (getFisiereIesireList()
											.getModel().getElementAt(i)));
								}
								// getFisiereIntrareList().setEnabled(false);
								disableOtherButtons(TransferOutButton);
								getManager().setFileOut(list);
								getManager().submitTranferOutJob();
								if (!getTimer().isRunning()) {
									getTimer().start();
								}
								fileOutTransferRunning = true;
							}
						}
					});

		}
		return TransferOutButton;
	}

	/**
	 * This method initializes DeleteButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton1() {
		if (DeleteButton == null) {
			DeleteButton = new JButton();
			DeleteButton.setBounds(new Rectangle(340, 410, 160, 30));
			DeleteButton.setText("Sterge de pe server");
			DeleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					ArrayList<String> listIn = new ArrayList<String>();
					ArrayList<String> listOut = new ArrayList<String>();
					if (getFisiereIesireList().getModel().getSize() > 0
							|| getFisiereIntrareList().getModel().getSize() > 0) {
						for (int i = 0; i < getFisiereIesireList().getModel()
								.getSize(); i++) {
							listIn.add((String) (getFisiereIesireList()
									.getModel().getElementAt(i)));
						}

						for (int i = 0; i < getFisiereIntrareList().getModel()
								.getSize(); i++) {
							listOut.add((String) (getFisiereIntrareList()
									.getModel().getElementAt(i)));
						}

						disableOtherButtons(DeleteButton);
						getManager().setFileIn(listIn);
						getManager().setFileOut(listOut);
						getManager().submitDeleteJob();
						if (!getTimer().isRunning()) {
							getTimer().start();
						}
						fileDeleteRunning = true;
					}
				}
			});
		}
		return DeleteButton;
	}

	/**
	 * This method initializes StartJobButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStartJobButton() {
		if (StartJobButton == null) {
			StartJobButton = new JButton();
			StartJobButton.setBounds(new Rectangle(500, 450, 140, 30));
			StartJobButton.setText("Start");
			StartJobButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try {
						JobManagementUtils.Utils.createProxy(Optiuni.getProxyFile(),
								Optiuni.getCertFile(), Optiuni.getKeyFile(), Optiuni
										.getPassword());
					} catch (Exception edd) {
						edd.printStackTrace();
					}
					if (!getTimer().isRunning()) {
						getTimer().start();
					}
					JobManagementServiceProxyClass proxyClass = new JobManagementServiceProxyClass(
							Optiuni.getJobFolderFullPath()
									+ "/"
									+ JobListComboBox.getModel()
											.getSelectedItem());
					System.out.println(Optiuni.getJobFolderFullPath() + "/"
							+ JobListComboBox.getModel().getSelectedItem()
							+ FisierRSL.getText());
					proxyClass.startJobManager(Optiuni.getProxyFile(),
							FisierRSL.getText());
				}
			});
		}
		return StartJobButton;
	}

	/**
	 * This method initializes StopJobButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStopJobButton1() {
		if (StopJobButton1 == null) {
			StopJobButton1 = new JButton();
			StopJobButton1.setBounds(new Rectangle(500, 500, 140, 30));
			StopJobButton1.setText("Stop");
			StopJobButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (!getTimer().isRunning()) {
						getTimer().start();
					}
					JobManagementServiceProxyClass proxyClass = new JobManagementServiceProxyClass(
							Optiuni.getJobFolderFullPath()
									+ "/"
									+ JobListComboBox.getModel()
											.getSelectedItem());

					proxyClass.stopJobManager();// (Optiuni.getProxyFile(),
					// FisierRSL.getText());
				}
			});
		}
		return StopJobButton1;
	}

	/**
	 * This method initializes StergeButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStergeButton() {
		if (StergeButton == null) {
			StergeButton = new JButton();
			StergeButton.setBounds(new Rectangle(500, 550, 140, 30));

			StergeButton.setText("Sterge");
			StergeButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (!getTimer().isRunning()) {
						getTimer().start();
					}
					if (JobListComboBox.getModel().getSelectedItem() != null
							&& !JobListComboBox.getModel().getSelectedItem()
									.toString().equals("")) {
						JobManagementServiceProxyClass proxyClass = new JobManagementServiceProxyClass(
								Optiuni.getJobFolderFullPath()
										+ "/"
										+ JobListComboBox.getModel()
												.getSelectedItem());

						proxyClass.destroy();
						ClientPanel.remove(JobListComboBox);
						JobListComboBox = null;
						ClientPanel.add(getJobListComboBox(), null);
						ClientPanel.repaint();
					}
				}
			});
		}
		return StergeButton;
	}

	/**
	 * This method initializes JobPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJobPanel() {
		if (JobPanel == null) {
			jLabel8 = new JLabel();
			jLabel8.setText("Numele Managerului");
			jLabel8.setBounds(new Rectangle(12, 17, 130, 33));
			JobPanel = new JPanel();
			JobPanel.setLayout(null);
			JobPanel.add(jLabel8, null);
			JobPanel.add(getNumeJobTextField(), null);
			JobPanel.add(getCreeaza(), null);
		}
		return JobPanel;
	}

	/**
	 * This method initializes NumeJobTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNumeJobTextField() {
		if (NumeJobTextField == null) {
			NumeJobTextField = new JTextField();
			NumeJobTextField.setBounds(new Rectangle(151, 19, 422, 29));
		}
		return NumeJobTextField;
	}

	/**
	 * This method initializes Creeaza
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreeaza() {
		if (Creeaza == null) {
			Creeaza = new JButton();
			Creeaza.setBounds(new Rectangle(137, 76, 271, 33));
			Creeaza.setText("Creeaza Job Manager");

			Creeaza.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// System.out.println("Create resource"); // TODO Create
					// Resource
					File jobsFolder = new File(Optiuni.getJobFolderFullPath());
					if (!jobsFolder.exists()) {
						try {
							System.out.println(jobsFolder.getAbsolutePath());
							jobsFolder.mkdir();
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}
					FactoryServiceProxyClass fact = new FactoryServiceProxyClass();
					fact.createResource(Optiuni.getJobFolderFullPath() + "/"
							+ getNumeJobTextField().getText());

					ClientPanel.remove(JobListComboBox);
					JobListComboBox = null;
					ClientPanel.add(getJobListComboBox(), null);
					ClientPanel.repaint();
				}
			});

		}
		return Creeaza;
	}

	/**
	 * This method initializes JobDirTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJobDirTextField() {
		if (JobDirTextField == null) {
			JobDirTextField = new JTextField();
			JobDirTextField.setBounds(new Rectangle(120, 200, 500, 20));
		}
		return JobDirTextField;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ClientGrafic client = new ClientGrafic();
		client.setDefaultCloseOperation(EXIT_ON_CLOSE);
		client.setVisible(true);
	}

	private void disableOtherButtons(JButton button) {
		TransferInButton.setEnabled(TransferInButton.equals(button));
		TransferOutButton.setEnabled(TransferOutButton.equals(button));
		DeleteButton.setEnabled(DeleteButton.equals(button));
	}

	private void enableAllButtons() {
		TransferInButton.setEnabled(true);
		TransferOutButton.setEnabled(true);
		DeleteButton.setEnabled(true);
	}

} // @jve:decl-index=0:visual-constraint="3,15"
